package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.ItemTypeQueryDto;
import com.group.eams.pojo.entity.ItemType;
import com.group.eams.service.ItemTypeService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;




/**
 * 物品类型(ItemType)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/itemType")
public class ItemTypeController  {
    /**
     * 服务对象
     */
    @Autowired
    private ItemTypeService itemTypeService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list( ItemTypeQueryDto itemTypeQueryDto) {
        Page<ItemType> page = new Page<>(itemTypeQueryDto.getPage(), itemTypeQueryDto.getLimit());
        LambdaQueryWrapper<ItemType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StrUtil.isEmpty(itemTypeQueryDto.getName()),
                        ItemType::getName, itemTypeQueryDto.getName())
                .like(!StrUtil.isEmpty(itemTypeQueryDto.getCode()),
                        ItemType::getCode, itemTypeQueryDto.getCode())
                .like(itemTypeQueryDto.getStatus()!=null,ItemType::getStatus,itemTypeQueryDto.getStatus());
        Page<ItemType> itemTypePage = itemTypeService.page(page, queryWrapper);
        return Result.buildSuccess(itemTypePage.getTotal(), itemTypePage.getRecords());

    }

    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id){
        ItemType itemType = itemTypeService.getById(id);
        return Result.buildSuccess(itemType);
    }

    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll() {
        return Result.buildSuccess(itemTypeService.list());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody ItemType itemType) {
        boolean result = itemTypeService.save(itemType);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody ItemType itemType) {
        LambdaUpdateWrapper<ItemType> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(ItemType::getId, itemType.getId());
        boolean  result = itemTypeService.update(itemType, queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id){
        LambdaQueryWrapper<ItemType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ItemType::getId, id);
        boolean result = itemTypeService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);

    }

    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){

        LambdaQueryWrapper<ItemType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ItemType::getId, batchDeleteDto.getIds());
        boolean result = itemTypeService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

}

